package com.jz.community.commview.view.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jz.community.commview.R;


/**
 * 异步加载数据提示框工具类
 */
public class CustomProgressDialog extends ProgressDialog {

    private AnimationDrawable mAnimation;
    private ImageView mImageView;
    private ImageView mBottomView;
    //进度条是否显示中间
    private boolean mIsCenterLoading = true;
    private Context mContext;
    public static final int LOAD_IMG_DEFUALT = -1;
    private int mResid = LOAD_IMG_DEFUALT;

    public CustomProgressDialog(Context context) {
        super(context, LOAD_IMG_DEFUALT);
        setCanceledOnTouchOutside(true);
    }

    public CustomProgressDialog(Context context, boolean isCenterLoading) {
        super(context);
        mContext = context;
        setCanceledOnTouchOutside(true);
        mIsCenterLoading = isCenterLoading;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        if (mIsCenterLoading) {
            mImageView.setVisibility(View.VISIBLE);
            mBottomView.setVisibility(View.GONE);
            // startAnimation(mImageView);
        } else {
            mImageView.setVisibility(View.GONE);
            mBottomView.setVisibility(View.VISIBLE);
            //   startAnimation(mBottomView);
        }
    }

    private void initView() {
        if (mResid == LOAD_IMG_DEFUALT) {
            //  this.mResid = R.anim.community_dialog_frame;
        }
        setContentView(R.layout.comm_progress_dialog);
        mImageView = findViewById(R.id.loadingIv);
        mBottomView = findViewById(R.id.bottom_loadingIv);
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0.0f;// 设置边框黑暗度为0
        window.setAttributes(lp);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        if (getContext() != null) {
            Glide.with(getContext())
                    .asGif()
                    .load(R.drawable.loading_default)
                    .into(mImageView);
        }
    }

    private void startAnimation(ImageView animationImg) {
        mAnimation = (AnimationDrawable) animationImg.getBackground();
        // 为了防止在onCreate方法中只显示第一帧的解决方案之一
        if (animationImg != null && mAnimation != null) {
            try {
                animationImg.post(new Runnable() {
                    @Override
                    public void run() {
                        mAnimation.start();
                    }
                });
            } catch (Exception e) {

            }

        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        try {
            mAnimation.stop();
        } catch (Exception e) {

        }
    }
}
